﻿using StarryEdge.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Parameter
{
    public class InitFileBase
    {
        protected IStarryEdgeLog _runLog;
        protected string _path;
        public InitFileBase(IStarryEdgeLog runLog, string directory, string filename)
        {
            _runLog = runLog;
            _path = Path.Combine(Environment.CurrentDirectory, directory, filename);
        }

        protected string Read()
        {
            try
            {
                return File.ReadAllText(_path, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                _runLog?.LogRun(string.Format("{0} 读取异常：{1}", this.GetType().Name, ex), Core.Enums.LogLevel.Error);
            }
            return string.Empty;
        }

        protected bool Save(string value)
        {
            try
            {
                File.WriteAllText(_path, value, Encoding.UTF8);
                return true;
            }
            catch (Exception ex)
            {
                _runLog?.LogRun(string.Format("{0} 保存异常：{1}", this.GetType().Name, ex), Core.Enums.LogLevel.Error);
            }
            return false;
        }
    }
}
